package com.example.listener;

import com.example.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * datetime : 2020/10/6 21:46
 * author : shenxiang
 */
@Component
public class PayListener {

    @Autowired
    private OrderService orderService;

    @RabbitListener(queues = "queue.pay.dlx")
    public void onMessage(Message message, Channel channel) throws IOException{
        // 获取队列中要取消的订单号
        String orderNo = new String(message.getBody(),message.getMessageProperties().getContentEncoding());
        System.out.println("要取消的订单号:"+orderNo);
        // 将表中此订单号设置为已取消
        boolean b = orderService.setCancelByOrderNo(orderNo);
        System.out.println("将订单号:"+orderNo+"设置为已取消"+(b ? "成功" : "失败")+".");
        // 消息确认
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    //@RabbitListener(queues = "queue.pay.biz")
//    @RabbitHandler
//    public void deliveryBiz(Message message,Channel channel) throws IOException{
//        String orderNo = new String(message.getBody(), message.getMessageProperties().getContentEncoding());
//        System.out.println("处理支付成功的订单号:"+orderNo);
//        boolean b = orderService.setPayedByOrderNo(orderNo);
//        System.out.println("将订单号:"+orderNo+"设置为已支付"+(b ? "成功" : "失败"));
//        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
//    }
}
